package com.hibiscusking.basic.controller;

import com.hibiscusking.basic.util.AjaxResult;
import com.hibiscusking.basic.util.FastdfsUtil;
import org.apache.velocity.shaded.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastdfs")
public class FastdfsController {
    @PostMapping
    public AjaxResult uploadFile(@RequestPart("file") MultipartFile multipartFile) throws IOException {
        String filename = multipartFile.getOriginalFilename();
        String extension = FilenameUtils.getExtension(filename);
        String path = FastdfsUtil.upload(multipartFile.getBytes(), extension);
        return AjaxResult.me().success(path);
    }
    @DeleteMapping
    public AjaxResult deleteFile(@RequestParam("filepath") String filePath) {
        String substring = filePath.substring(1);
        String groupName = substring.substring(0, substring.indexOf("/"));
        String remotePath = substring.substring(substring.indexOf("/") + 1 );
        FastdfsUtil.delete(groupName,remotePath);
        return AjaxResult.me().success();
    }
}
